package com.metacube.questionbank.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.metacube.questionbank.dao.QuestionDAO;
import com.metacube.questionbank.dto.CommentDTO;
import com.metacube.questionbank.service.Utility;
import com.metacube.questionbank.service.Validator;

@Controller
@RequestMapping("/comment")
public class PostCommentController {
	
	@Autowired
	Validator validate;
	
	@Autowired
	QuestionDAO qDAO;
	
	@Autowired
	Utility utility;
	
	@RequestMapping(value = "/postQuestionComment",  method = RequestMethod.POST)
	public ModelAndView postQuestionComment(@ModelAttribute CommentDTO commentDTO,@RequestParam("qId") int qId, HttpSession session, HttpServletRequest req) {
		commentDTO.setCommentBody(req.getParameter("commentBody"));
		System.out.println("a body "+commentDTO.getCommentBody());
		
		
		
		String eMail = (String) session.getAttribute("sessionMail");

		commentDTO.setqId(qId);
		
		System.out.println("QDTO"+commentDTO.getqId());
			
		if(validate.checkComment(commentDTO)) {
			
			return new ModelAndView("MetaQBank/postAnswer.jsp","errorMessage", "Comment Body Can't be empty");
			
		} else {
			
		if (utility.inItComment(commentDTO, eMail)) {
				
				ModelAndView model = new ModelAndView("postAnswer.jsp");
				
				System.out.println();
				
				model.addObject("commentDTO", commentDTO);
				
				return model;
				
			} else {
				
				return new ModelAndView("MetaQBank/postAnswer","errorMessage","Your answer not valid");		
			}
				
		}
	
			
	}
	
	@RequestMapping(value = "/postAnswerComment",  method = RequestMethod.GET)
	public ModelAndView postAnswerComment(@ModelAttribute CommentDTO commentDTO,@RequestParam("aId") int aId ,@RequestParam("body") String CommentBody ,HttpSession session, HttpServletRequest req) {
		
	commentDTO.setCommentBody(CommentBody);
		
		
		
		String eMail = (String) session.getAttribute("sessionMail");
		
		commentDTO.setaId(aId);
		
		System.out.println("ADTO"+commentDTO.getaId());
			
		if(validate.checkComment(commentDTO)) {
			
			return new ModelAndView("postAnswer.jsp","errorMessage", "Comment Body Can't be empty");
			
		} else {
			
		if (utility.inItAnswerComment(commentDTO, eMail)) {
				
				ModelAndView model = new ModelAndView("postAnswer.jsp");
				
				System.out.println("printing");
				
				model.addObject("commentDTO", commentDTO);
				
				return model;
				
			} else {
				
				return new ModelAndView("postAnswer.jsp","errorMessage","Your answer not valid");		
			}
				
		}
	
			
	}


}
